package cn.toma.controller.sys;

import cn.toma.common.annotation.Log;
import cn.toma.common.exception.FfBootException;
import cn.toma.common.utils.StringUtils;
import cn.toma.common.vo.QueryRequest;
import cn.toma.common.vo.StringPool;
import cn.toma.config.web.utli.BaseController;
import cn.toma.config.web.utli.FfBootResponse;
import cn.toma.model.sys.Role;
import cn.toma.model.sys.RoleMenu;
import cn.toma.service.sys.RoleMenuServie;
import cn.toma.service.sys.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Validated
@RestController
@RequestMapping("role")
public class RoleController extends BaseController
{

	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleMenuServie roleMenuServie;

	private String message;

	@RequiresPermissions("role:view")
	@GetMapping("list")
	public FfBootResponse getRoleList()
	{
		List<Role> all = roleService.findAll();
		return new FfBootResponse().data(all);
	}

	@GetMapping
	@RequiresPermissions("role:view")
	public Map<String, Object> roleList(QueryRequest queryRequest, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize, Role role)
	{
		Page<Role> objects = PageHelper.startPage(pageNum, pageSize);
		StringUtils.sortOrderBy(queryRequest, objects);
		roleService.findRolePage(role);
		return getDataTable(objects.toPageInfo());
	}

	@GetMapping("check/{roleName}")
	public boolean checkRoleName(@NotBlank(message = "{required}") @PathVariable String roleName)
	{
		Role result = this.roleService.findByName(roleName);
		return result == null;
	}

	@GetMapping("menu/{roleId}")
	public List<String> getRoleMenus(@NotBlank(message = "{required}") @PathVariable String roleId)
	{
		List<RoleMenu> list = this.roleMenuServie.getRoleMenusByRoleId(roleId);
		return list.stream().map(roleMenu -> String.valueOf(roleMenu.getMenuId())).collect(Collectors.toList());
	}

	@Log("新增角色")
	@PostMapping
	@RequiresPermissions("role:add")
	public void addRole(@Valid Role role) throws FfBootException
	{
		try {
			this.roleService.createRole(role);
		} catch (Exception e) {
			message = "新增角色失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

	@Log("删除角色")
	@DeleteMapping("/{roleIds}")
	@RequiresPermissions("role:delete")
	public void deleteRoles(@NotBlank(message = "{required}") @PathVariable String roleIds) throws FfBootException
	{
		try {
			String[] ids = roleIds.split(StringPool.COMMA);
			this.roleService.deleteRoles(ids);
		} catch (Exception e) {
			message = "删除角色失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

	@Log("修改角色")
	@PutMapping
	@RequiresPermissions("role:update")
	public void updateRole(Role role) throws FfBootException
	{
		try {
			this.roleService.updateRole(role);
		} catch (Exception e) {
			message = "修改角色失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

//    @PostMapping("excel")
//    @RequiresPermissions("role:export")
//    public void export(QueryRequest queryRequest, Role role, HttpServletResponse response) throws FfBootException {
//        try {
//            List<Role> roles = this.roleService.findRoles(role, queryRequest).getRecords();
//            ExcelKit.$Export(Role.class, response).downXlsx(roles, false);
//        } catch (Exception e) {
//            message = "导出Excel失败";
//            log.error(message, e);
//            throw new FfBootException(message);
//        }
//    }
}
